// TaskManager.cpp
#include <QApplication>
#include "ElaApplication.h"
#include "ElaWindow.h"
#include <QWidget>
#include <QListWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QCheckBox>
#include <QLabel>

class TaskPage : public QWidget
{
    Q_OBJECT
public:
    TaskPage(QWidget* parent = nullptr) : QWidget(parent)
    {
        setupUI();
        setupConnections();
    }
    
    void addTask(const QString& task)
    {
        auto item = new QListWidgetItem(task);
        auto checkbox = new QCheckBox(task);
        
        m_listWidget->addItem(item);
        m_listWidget->setItemWidget(item, checkbox);
        
        connect(checkbox, &QCheckBox::stateChanged, this, [this, item](int state) {
            if (state == Qt::Checked) {
                item->setBackground(QColor(144, 238, 144)); // 浅绿色
                m_completedCount++;
            } else {
                item->setBackground(Qt::white);
                m_completedCount--;
            }
            emit taskProgressChanged(m_completedCount, m_listWidget->count());
        });
    }
    
signals:
    void taskProgressChanged(int completed, int total);

private:
    void setupUI()
    {
        auto layout = new QVBoxLayout(this);
        
        // 输入区域
        auto inputLayout = new QHBoxLayout();
        m_taskInput = new QLineEdit(this);
        m_taskInput->setPlaceholderText("输入新任务...");
        m_addButton = new QPushButton("添加", this);
        
        inputLayout->addWidget(m_taskInput);
        inputLayout->addWidget(m_addButton);
        layout->addLayout(inputLayout);
        
        // 任务列表
        m_listWidget = new QListWidget(this);
        layout->addWidget(m_listWidget);
        
        // 统计信息
        m_statsLabel = new QLabel("总任务: 0, 已完成: 0", this);
        layout->addWidget(m_statsLabel);
    }
    
    void setupConnections()
    {
        connect(m_addButton, &QPushButton::clicked, this, [this]() {
            if (!m_taskInput->text().isEmpty()) {
                addTask(m_taskInput->text());
                m_taskInput->clear();
                updateStats();
            }
        });
        
        connect(m_taskInput, &QLineEdit::returnPressed, this, [this]() {
            m_addButton->click();
        });
        
        connect(this, &TaskPage::taskProgressChanged, this, [this](int completed, int total) {
            updateStats();
        });
    }
    
    void updateStats()
    {
        m_statsLabel->setText(QString("总任务: %1, 已完成: %2")
                             .arg(m_listWidget->count())
                             .arg(m_completedCount));
    }

private:
    QListWidget* m_listWidget;
    QLineEdit* m_taskInput;
    QPushButton* m_addButton;
    QLabel* m_statsLabel;
    int m_completedCount = 0;
};

class TaskManagerWindow : public ElaWindow
{
public:
    TaskManagerWindow(QWidget* parent = nullptr) : ElaWindow(parent)
    {
        setupUI();
        setupConnections();
    }

private:
    void setupUI()
    {
        setWindowTitle("任务管理器");
        
        m_taskPage = new TaskPage();
        
        // 添加任务管理节点
        QString tasksKey;
        addExpanderNode("任务管理", tasksKey, ElaIconType::Globe);
        
        addPageNode("今日任务", m_taskPage, tasksKey, ElaIconType::EllipsisStrokeVertical);
        
        // 添加一些示例任务
        m_taskPage->addTask("阅读项目文档");
        m_taskPage->addTask("完成代码审查");
        m_taskPage->addTask("准备会议材料");
        
        // 设置关键点（未完成任务数）
        setNodeKeyPoints(tasksKey, 3);
        
        // 用户信息
        setUserInfoCardTitle("开发者");
        setUserInfoCardSubTitle("tasks@example.com");
    }
    
    void setupConnections()
    {
        // 监听任务进度变化，更新关键点
        connect(m_taskPage, &TaskPage::taskProgressChanged, this, 
                [this](int completed, int total) {
            int remaining = total - completed;
            setNodeKeyPoints("tasksKey", remaining); // 更新未完成任务数
        });
    }

private:
    TaskPage* m_taskPage;
};

int main(int argc, char *argv[])
{
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);  // 启用高 DPI (界面布局和几何)
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);                                               //  启用自动高 DPI 缩放 (图标和图片)
    QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);  // 使用精确的缩放因子，不进行四舍五入
#else
    // 根据实际屏幕缩放比例更改
    qputenv("QT_SCALE_FACTOR", "1.5");
#endif
#endif
    QApplication app(argc, argv);
    eApp->init();  // 初始化资源、字体等
    
    TaskManagerWindow window;
    window.resize(1000, 700);
    window.moveToCenter();
    window.show();
    
    return app.exec();
}

#include "TaskManager.moc"  // ← 添加这一行